package com.yh.homestay.controller;

import com.yh.homestay.common.StaffEditResultG;
import com.yh.homestay.pojo.UserStaff;
import com.yh.homestay.service.UserStaffServiceG;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName UserStaffController
 * @Description 账号登出Controller
 * @Date 2020/1/6 19:27
 */
@Controller
@Api(description = "账号登出")
public class UserStaffControllerG {

    @Autowired
    private UserStaffServiceG userStaffService;

    /**
    * @Date: 2020/1/8 14:03
    * @Description: 账号登出，清除cookie，session
    * @param request
     * @param response
    * @return: java.lang.String
    */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ApiOperation(value = "账号登出", notes = "账号登出，清除cookie，session")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies
        ) {
            cookie.setMaxAge(0);
//            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return "login";
    }
    /**
    * @Date: 2020/1/13 18:44
    * @Description: 用户信息回显
    * @param id 
    * @return: com.yh.homestay.common.StaffEditResultG
    */ 
    @RequestMapping(value = "/staff/edit/selectOne/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户信息回显", notes = "根据用户id进行信息回显")
    //POST http://localhost/staff/edit/selectOne/1 HTTP/1.1
    public StaffEditResultG editStaff(@PathVariable("id") String id){
        try{
            UserStaff userStaff = userStaffService.selectOne(Integer.parseInt(id));
             return StaffEditResultG.ok(200,userStaff);
        }catch (Exception e){
            e.printStackTrace();
            return StaffEditResultG.build(500,"error");
        }
    }

}
